<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<title>iScroll demo: simple</title>

	<script type="text/javascript" src="iscroll-probe.js"></script>

	<script type="text/javascript">

		var myScroll,   //iscroll实例
				pullDownEl, //下拉元素
        pullDownOffset,//下拉元素的偏移量
				pullUpEl,
        pullUpOffset,
        _maxScrollY, //下滚动最大滚动距离
        generatedCount = 0;//模拟加载数据的行编号
    //下拉加载数据
		function pullDownAction () {
		  //真实情况
		  /*$.ajax({
		    url:'',
        data:{},
        dataType:'',
        success:function(arr){
          for (i=0; i<arr.length; i++) {
            li = document.createElement('li');
            li.innserHTML = '...';
            el.insertBefore(li, el.childNodes[0]);
          }
         if(myScroll){
           myScroll.refresh();		// 必须要通知iscroll实例更新
         }
        }
      });*/

			setTimeout(function () {	//  模拟数据加载
				var el, li, i;
				el = document.getElementById('thelist');

				for (i=0; i<3; i++) {
					li = document.createElement('li');
					li.innerText = '插入的行 ' + (++generatedCount);
					el.insertBefore(li,el.childNodes[0]);
				}

				if(myScroll){
					myScroll.refresh();		// 必须要通知iscroll实例更新
				}
			}, 1000);	//

		}
    //上拉加载数据
		function pullUpAction () {
			setTimeout(function () {	// 模拟
				var el, li, i;
				el = document.getElementById('thelist');

				for (i=0; i<3; i++) {
					li = document.createElement('li');
					li.innerText = '插入的行 ' + (++generatedCount);
					el.appendChild(li, el.childNodes[0]);
				}
				if(myScroll){
					myScroll.refresh();		//对iscroll更新
				}
			}, 1000);	//
		}

		function loaded() {
		  //计算上拉下拉元素的偏移量
			pullDownEl = document.getElementById('pullDown');
			if(pullDownEl){
				pullDownOffset = pullDownEl.offsetHeight;
			}else{
				pullDownOffset = 0;
			}
			pullUpEl = document.getElementById('pullUp');
			if(pullUpEl){
				pullUpOffset = pullUpEl.offsetHeight;
			}else{
				pullUpOffset = 0;
			}

			//iscroll配置
			var myOptions = {
					 mouseWheel: true,   //允许鼠标
				     scrollbars: true,   //出现滚动条
					 fadeScrollbars: true, //渐隐
					 probeType:1,//滚动不繁忙的时候触发
					 startY:-pullDownOffset   //起始scroll的y坐标
			};

			myScroll = new IScroll("#wrapper",myOptions);
//			console.log("maxScrollY-之前:"+myScroll.maxScrollY);

			_maxScrollY = myScroll.maxScrollY = myScroll.maxScrollY + pullDownOffset;
			//console.log("maxScrollY-之后:"+myScroll.maxScrollY);

			var isScrolling = false;//滚动开关 false


			//Event: 滚动开始时
			myScroll.on("scrollStart",function(){
				if(this.y==this.startY){
					isScrolling = true;   //打开滚动开关
				}
			});

			//Event: 滚动中
			myScroll.on("scroll",function(){
				if (this.y > 5 && !pullDownEl.className.match('flip')) {//可以刷新了
					pullDownEl.className = 'flip';//箭头冲上
					pullDownEl.querySelector('.pullDownLabel').innerHTML = '松开刷新...';
					this.minScrollY = 0;//记录 松手了，滚动到的y坐标 0
				} else if (this.y < 5 && pullDownEl.className.match('flip')) {//不刷新
					pullDownEl.className = '';//箭头冲下
					pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
					this.minScrollY = -pullDownOffset;//记录 松手了，滚动到的y坐标 0
				} else if (this.y <= (_maxScrollY-pullUpOffset) && !pullUpEl.className.match('flip')) {//可以加载了
					pullUpEl.className = 'flip';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '松开加载';
					this.maxScrollY = this.maxScrollY-pullUpOffset;
				} else if (this.y > (_maxScrollY-pullUpOffset) && pullUpEl.className.match('flip')) {//不加载
					pullUpEl.className = '';
					pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载';
					this.maxScrollY = this.maxScrollY + pullUpOffset;
				}

				//TODO
				console.log("y:"+this.y);
			});

			//Event: 滚动结束
			myScroll.on("scrollEnd", function() {
//				  console.log("scroll end");
//				  console.log("maxScrollY-3:"+this.maxScrollY);
				 if (pullDownEl && !pullDownEl.className.match("flip") && this.y > this.options.startY) {
						 console.log("复位");
						 this.scrollTo(0, this.options.startY,800);//不刷新，回到最初
			      }
				 else if (pullDownEl && pullDownEl.className.match("flip")){//要刷新
							 pullDownEl.className = "loading";
							  pullDownEl.querySelector(".pullDownLabel").innerHTML = "刷新中...";
							 // Execute custom function (ajax call?)
						  if (isScrolling) {
//								console.log("before pull down action:"+this.y);
								 pullDownAction();
//								   console.log("after pull down action:"+this.y);
						   }
				 }
				  else if (pullUpEl && pullUpEl.className.match("flip")) {
							 //console.log("pull up loading");
							 pullUpEl.className = "loading";
							 pullUpEl.querySelector(".pullUpLabel").innerHTML = "加载中..";
								  if (isScrolling) {
//										  console.log("before pull up action:"+this.y);
										 pullUpAction();
//										 console.log("after pull up action:"+this.y);
								  }
					         }
				isScrolling = false;  //滚动结束时，关闭滚动开关
			 });
			//Event: DOM更新时,滚动到最新位置
			myScroll.on("refresh", function() {
//				   console.log("maxScrollY-4:"+this.maxScrollY);
				   _maxScrollY = this.maxScrollY = this.maxScrollY+pullUpOffset;
//				   console.log("maxScrollY-5:"+this.maxScrollY);

				   if (pullDownEl  && pullDownEl.className.match("loading")) {
							  pullDownEl.className = "";
							  pullDownEl.querySelector(".pullDownLabel").innerHTML = "下拉刷新2";
							  this.scrollTo(0,this.options.startY,0);
					   } else if (pullUpEl && pullUpEl.className.match("loading")) {
							  pullUpEl.className = "";
							  pullUpEl.querySelector(".pullUpLabel").innerHTML = "上拉加载2";
							 this.scrollTo(0,this.maxScrollY,0);
					   }

//				   console.log("refresh finished!");
			  });



			setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
		}

		document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

	   // document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
	</script>

	<style type="text/css" media="all">
		body,ul,li {
			padding:0;
			margin:0;
			border:0;
		}

		body {
			font-size:12px;
			-webkit-user-select:none;
			-webkit-text-size-adjust:none;
			font-family:helvetica;
		}

		#header {
			position:absolute; z-index:2;
			top:0; left:0;
			width:100%;
			height:45px;
			line-height:45px;
			background-color:#d51875;
			background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));
			background-image:-moz-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
			background-image:-o-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
			padding:0;
			color:#eee;
			font-size:20px;
			text-align:center;
		}

		#header a {
			color:#f3f3f3;
			text-decoration:none;
			font-weight:bold;
			text-shadow:0 -1px 0 rgba(0,0,0,0.5);
		}

		#footer {
			position:absolute; z-index:2;
			bottom:0; left:0;
			width:100%;
			height:48px;
			background-color:#222;
			background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));
			background-image:-moz-linear-gradient(top, #999, #666 2%, #222);
			background-image:-o-linear-gradient(top, #999, #666 2%, #222);
			padding:0;
			border-top:1px solid #444;
		}

		#wrapper {
			position:absolute; z-index:1;
			top:45px; bottom:48px; left:-9999px;
			width:100%;
			background:#aaa;
			overflow:hidden;
		}

		#scroller {
			position:absolute; z-index:1;
			/*	-webkit-touch-callout:none;*/
			-webkit-tap-highlight-color:rgba(0,0,0,0);
			width:100%;
			padding:0;
		}

		#scroller ul {
			list-style:none;
			padding:0;
			margin:0;
			width:100%;
			text-align:left;
		}

		#scroller li {
			padding:0 10px;
			height:40px;
			line-height:40px;
			border-bottom:1px solid #ccc;
			border-top:1px solid #fff;
			background-color:#fafafa;
			font-size:14px;
		}

		#myFrame {
			position:absolute;
			top:0; left:0;
		}



		/**
         *
         * Pull down styles
         *
         */
		#pullDown, #pullUp {
			background:#fff;
			height:40px;
			line-height:40px;
			padding:5px 10px;
			border-bottom:1px solid #ccc;
			font-weight:bold;
			font-size:14px;
			color:#888;
		}
		#pullDown .pullDownIcon, #pullUp .pullUpIcon  {
			display:block; float:left;
			width:40px; height:40px;
			background:url(pull-icon@2x.png) 0 0 no-repeat;
			-webkit-background-size:40px 80px; background-size:40px 80px;
			-webkit-transition-property:-webkit-transform;
			-webkit-transition-duration:250ms;
		}
		#pullDown .pullDownIcon {
			-webkit-transform:rotate(0deg) translateZ(0);
		}
		#pullUp .pullUpIcon  {
			-webkit-transform:rotate(-180deg) translateZ(0);
		}

		#pullDown.flip .pullDownIcon {
			-webkit-transform:rotate(-180deg) translateZ(0);
		}

		#pullUp.flip .pullUpIcon {
			-webkit-transform:rotate(0deg) translateZ(0);
		}

		#pullDown.loading .pullDownIcon, #pullUp.loading .pullUpIcon {
			background-position:0 100%;
			-webkit-transform:rotate(0deg) translateZ(0);
			-webkit-transition-duration:0ms;

			-webkit-animation-name:loading;
			-webkit-animation-duration:2s;
			-webkit-animation-iteration-count:infinite;
			-webkit-animation-timing-function:linear;
		}

		@-webkit-keyframes loading {
			from { -webkit-transform:rotate(0deg) translateZ(0); }
			to { -webkit-transform:rotate(360deg) translateZ(0); }
		}

	</style>
</head>
<body onload="loaded()">

<div id="header"><a href="http://cubiq.org/iscroll">iScroll</a></div>
<div id="wrapper">
	<div id="scroller">
		<div id="pullDown">
			<span class="pullDownIcon"></span><span class="pullDownLabel">下拉刷新0...</span>
		</div>

		<ul id="thelist">
			<li>Pretty row 1</li>
			<li>Pretty row 2</li>
			<li>Pretty row 3</li>
			<li>Pretty row 4</li>
			<li>Pretty row 5</li>
			<li>Pretty row 6</li>
			<li>Pretty row 7</li>
			<li>Pretty row 8</li>
			<li>Pretty row 9</li>
			<li>Pretty row 10</li>
			<li>Pretty row 11</li>
			<li>Pretty row 12</li>
			<li>Pretty row 13</li>
			<li>Pretty row 14</li>
			<li>Pretty row 15</li>
			<li>Pretty row 16</li>
			<li>Pretty row 17</li>
			<li>Pretty row 18</li>
			<li>Pretty row 19</li>
			<li>Pretty row 20</li>
			<li>Pretty row 21</li>
			<li>Pretty row 22</li>
			<li>Pretty row 23</li>
			<li>Pretty row 24</li>
			<li>Pretty row 25</li>
			<li>Pretty row 26</li>
			<li>Pretty row 27</li>
			<li>Pretty row 28</li>
			<li>Pretty row 29</li>
			<li>Pretty row 30</li>
			<li>Pretty row 31</li>
			<li>Pretty row 32</li>
			<li>Pretty row 33</li>
			<li>Pretty row 34</li>
			<li>Pretty row 35</li>
			<li>Pretty row 36</li>
			<li>Pretty row 37</li>
			<li>Pretty row 38</li>
			<li>Pretty row 39</li>
			<li>Pretty row 40</li>
		</ul>
		<div id="pullUp">
			<span class="pullUpIcon"></span><span class="pullUpLabel">上拉加载0</span>
		</div>
	</div>
</div>
<div id="footer"></div>

</body>
</html>